<?php
/**
 * 用户管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Member;

use App\Admin\Actions\Member\BatchOnShelf;
use App\Admin\Actions\Member\BatchOffShelf;
use App\Admin\Actions\Member\ClearPoints;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\UserAddressModel;
use App\Models\yb\UserCouponModel;
use App\Models\yb\UserModel;
use App\Models\yb\UserWalletRecordModel;
use App\Models\yb\UserWaterTicketModel;
use App\Models\yb\UserWaterTicketRecordModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;


class UserController extends Controller
{
    use ModelForm;

    protected $header = '用户管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    /**
     * 详情
     * @return Content
     */
    public function detail(Request $request){

        $user_id = $request->get('user_id');
        return Admin::content(function (Content $content)use($user_id) {

            $tab = new Tab();
            $tab->add('水票', $this->UserWaterTicket($user_id));
            $tab->add('优惠券',  $this->UserCoupon($user_id));
            $tab->add('积分',  $this->UserWallet($user_id));
            $content->header($this->header);
            $content->description('详情');
            $content->body($this->address($user_id));
            $content->body($tab->render());
        });
    }

    /**
     * 详情
     * @return Content
     */
    public function water_ticket_record(Request $request){

        $user_id = $request->get('user_id');
        return Admin::content(function (Content $content)use($user_id) {
            $tab = new Tab();
            $tab->add('消费', $this->water_ticket_record_list($user_id,1));
            $tab->add('购买',  $this->water_ticket_record_list($user_id,2));
            $tab->add('转让',  $this->water_ticket_record_list($user_id,3));
            $content->header($this->header);
            $content->description('详情');
            $content->body($tab->render());
        });
    }

    public function grid(){

        $grid = new Grid(new UserModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','ID')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('nickname','昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('avatar', '头像')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);


        $grid->column('phone','手机号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('guests_promotion','获客/推广')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('surplus_points','剩余积分')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('withdrawal_points','已提现')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('status','状态')->using(DataDictionaryModel::Status, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('created_at','注册时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        $grid->column('action_id','操作')->display(function($filed){

            return '<a href="detail?user_id='.$filed.'" class="grid-row-edit">
                        <button type="button" class="btn btn-primary btn-xs btn-flat">详情</button>
                    </a>
                    <a href="/admin/order/order/manage?user_id='.$this->id.'" class="grid-row-edit">
                        <button type="button" class="btn btn-info btn-xs btn-flat">订单</button>
                    </a>
                    <a href="/admin/member/user/withdrawal?&user_nickname='.$this->nickname.'&status=&user_phone=&created_at%5Bstart%5D=&created_at%5Bend%5D=" data-id="52625" class="grid-row-delete">
                        <button type="button" class="btn btn-danger btn-xs btn-flat">提现</button>
                    </a>';
        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('nickname','昵称');
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('phone','电话');
                $filter->between('created_at', '注册时间')->datetime();

            });

            $filter->disableIdFilter();
        });

        $grid->tools(function ($tools) {
            $tools->append(new BatchOnShelf());
            $tools->append(new BatchOffShelf());
            $tools->append(new ClearPoints());
        });


        $grid->disableActions();

        $grid->disableCreateButton();

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * 水票明细
     * @return Content
     */
    public function UserWaterTicket($user_id){

        $grid = new Grid(new UserWaterTicketModel());

        $grid->model()->where(['user_id'=>$user_id])->orderBy('created_at', 'DESC');

        $grid->column('goods_specs','规格')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('cumulative_number','累计购买')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('cover_transferor_number','被转让获得')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('transferor_number','转让消耗')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('used_number','已使用数量')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('number','剩余数量')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('user_id','操作')->display(function($filed){
            return '<a href="water/ticket/record?user_id='.$filed.'" class="grid-row-edit">
                        <button type="button" class="btn btn-info btn-xs btn-flat">明细</button>
                    </a>';
        });

        $grid->disableCreateButton();

        $grid->disableFilter();

        $grid->disableRowSelector();

        $grid->disableActions();

        $grid->disableExport();

        return $grid->render();

    }

    /**
     * 优惠券明细
     * @return Content
     */
    public function UserCoupon($user_id){

        $grid = new Grid(new UserCouponModel());

        $grid->model()->where(['user_id'=>$user_id])->orderBy('created_at', 'DESC');

        $grid->column('coupon_id','优惠券编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('coupon_name','优惠券名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('full_reduce','满减')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('is_used','是否使用')->using(['0'=>'未使用','1'=>'已使用'], '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('usage_time','使用时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('status','是否过期')->using(['0'=>'已过期','1'=>'正常使用'], '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');


        $grid->disableCreateButton();

        $grid->disableFilter();

        $grid->disableRowSelector();

        $grid->disableActions();

        $grid->disableExport();

        return $grid->render();

    }

    /**
     * 积分明细
     * @return Content
     */
    public function UserWallet($user_id){

        $grid = new Grid(new UserWalletRecordModel());

        $grid->model()->where(['user_id'=>$user_id])->orderBy('created_at', 'DESC');

        $grid->column('id','ID')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('nickname','用户昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('phone','电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('avatar', '用户头像')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('status','类型')->using(['0'=>'扣除','1'=>'增加'], '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('amount','金额')->display(function($filed){
            return empty($filed) ? 0: $filed;
        });

        $grid->column('remark','备注')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('created_at','操作时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->disableCreateButton();

        $grid->disableFilter();

        $grid->disableRowSelector();

        $grid->disableActions();

        $grid->disableExport();

        return $grid->render();

    }


    /**
     *  用户详情+地址详情
     * @return Content
     */
    public function address($user_id){
        $user_data = UserModel::where(['id'=>$user_id])->first();
        $address_list = UserAddressModel::where(['user_id'=>$user_id])->get();
        return view('admin.member.detail',['user_data'=>$user_data,'address_list'=>$address_list]);
    }

    /**
     *  水票明细
     * @return Content
     */
    public function water_ticket_record_list($user_id,$type){

        $grid = new Grid(new UserWaterTicketRecordModel());

        $grid->model()->where(['user_id'=>$user_id,'type'=>$type])->orderBy('created_at', 'DESC');

        $grid->column('id','ID')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_specs','规格')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('number','数量')->display(function($filed){
            if($this->type==1){
                return empty($filed) ? config('view.EmptyData') : "-".$filed;
            }elseif($this->type == 2){
                return empty($filed) ? config('view.EmptyData') : "+".$filed;
            }elseif($this->type == 3){
                if($this->status == 0){
                    return empty($filed) ? config('view.EmptyData') : "-".$filed;
                }else{
                    return empty($filed) ? config('view.EmptyData') : "+".$filed;
                }
            }
        });

        if($type == 3){
            $grid->column('transferor_phone','对方手机号')->display(function($filed){
                return empty($filed) ? config('view.EmptyData') : $filed;
            });
        }

        $grid->column('created_at','操作时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->disableCreateButton();

        $grid->disableFilter();

        $grid->disableRowSelector();

        $grid->disableActions();

        $grid->disableExport();

        return $grid->render();

    }


}
